package com.frank.loadinglibrary.special.cool.letter;


import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;

/**
 * Created by Weiwu on 16/2/19.
 */
public class LLetter extends BaseLetter {

    private Paint mPaint;

    //L上面的点
    private Point mFirstPoint;
    //转折处的点
    private Point mSecondPoint;
    //结尾点
    private Point mThirdPoint;

    //竖线
    private AnimatorValue mFirstLineAnimator;
    //横线
    private AnimatorValue mSecondLineAnimator;

    private int mStrokeWidth = 20;
    private int mLength = 140;
    private int mWidth = 80;

    //横线画完的标志
    private boolean mIsFirstFinish = false;

    public LLetter(float x, float y) {
        super(x, y);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setStrokeWidth(mStrokeWidth);


        mFirstPoint = new Point(x - mWidth / 2 + mStrokeWidth / 2, y - mLength / 4 * 3);
        mSecondPoint = new Point(mFirstPoint);
        mThirdPoint = new Point(x - mWidth / 2 + mStrokeWidth / 2, y + mLength / 4 + mStrokeWidth / 2);

    }

    @Override
    public void reset() {
        super.reset();
        mStrokeWidth = 20;
        mLength = 140;
        mWidth = 80;
        if (mPaint != null) {
            mPaint.reset();
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL_STYLE);
            mPaint.setStrokeWidth(mStrokeWidth);
        }
        mIsFirstFinish = false;
        if (mFirstPoint != null) {
            mFirstPoint.modify(mCurX - mWidth / 2 + mStrokeWidth / 2, mCurY - mLength / 4 * 3);
        }
        if (mSecondPoint != null) {
            mSecondPoint.modify(mFirstPoint);
        }

        if (mThirdPoint != null) {
            mThirdPoint.modify(mCurX - mWidth / 2 + mStrokeWidth / 2, mCurY + mLength / 4 + mStrokeWidth / 2);
        }
    }

    @Override
    public void startAnim() {
        mFirstLineAnimator = new AnimatorValue();
        mFirstLineAnimator.setDuration(mDuration / 2);
        mFirstLineAnimator.setValueUpdateListener((animatorValue, v) -> {
            float factor = v;
            float my = (mFirstPoint.getPointY() + (mLength + mStrokeWidth) * factor);
            mSecondPoint.modify(mSecondPoint.getPointX(), my);
        });
        mFirstLineAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                mIsFirstFinish = true;
                mSecondLineAnimator.start();
            }
        });

        mSecondLineAnimator = new AnimatorValue();
        mSecondLineAnimator.setDuration(mDuration / 2);
        mSecondLineAnimator.setValueUpdateListener((ani, v) -> {
            float factor = v;
            float mx = (int) (mSecondPoint.getPointX() + mWidth * factor);
            mThirdPoint.modify(mx, mThirdPoint.getPointY());
        });

        mSecondLineAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (animationCallBack != null) {
                    animationCallBack.onLetterAnimatorEnd();
                }
            }
        });

        mFirstLineAnimator.start();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        canvas.drawLine(mFirstPoint.getPointX(), mFirstPoint.getPointY(), mSecondPoint.getPointX(), mSecondPoint.getPointY(), mPaint);
        if (mIsFirstFinish) {
            //减去了线粗导致的偏移
            canvas.drawLine(mSecondPoint.getPointX() - mStrokeWidth / 2, mSecondPoint.getPointY() - mStrokeWidth / 2, mThirdPoint.getPointX(), mThirdPoint.getPointY(), mPaint);
        }
    }
}
